using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using BA.DAL;

namespace BlazeApps.Library.Template
{
    [Serializable()]
    public class TemplateParent : Csla.ReadOnlyListBase<TemplateParent, TemplateChild>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Authorization Rules

        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in TemplateParent
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("TemplateParentViewGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private TemplateParent()
        { /* require use of factory method */
        }

        public static TemplateParent GetTemplateParent()
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a TemplateParent");
            return DataPortal.Fetch<TemplateParent>(new FilterCriteria());
        }

        public static TemplateParent GetTemplateParent(Guid Templateid)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a TemplateParent");
            return DataPortal.Fetch<TemplateParent>(new FilterCriteria(Templateid));
        }

        #endregion //Factory Methods

        #region Data Access

        #region Filter Criteria
        [Serializable()]
        private class FilterCriteria
        {
            public Guid Templateid;
         
            public FilterCriteria()
            {

            }

            public FilterCriteria(Guid Templateid)
            {
                this.Templateid = Templateid;
            }
        }
        #endregion //Filter Criteria

        #region Data Access - Fetch
        private void DataPortal_Fetch(FilterCriteria criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            ExecuteFetch(criteria);

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        private void ExecuteFetch(FilterCriteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@templateid", criteria.Templateid);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.ListTemplates.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                while (dr.Read())
                    this.Add(TemplateChild.GetTemplateChild(dr));
            }
        }
        #endregion //Data Access - Fetch
        #endregion //Data Access
    }
}
